<form class="form-horizontal" method="post">
  <input type="hidden" name="page" value="ban_be" />
  <input type="hidden" name="friend[friend_id]" value="<?= isset($friend['friend_id'])? $friend['friend_id'] : '' ?>" />
  <fieldset>
    <legend><?= isset($text_action)? $text_action : '' ?></legend>
    <div class="control-group">
      <label class="control-label">Họ tên</label>
      <div class="controls docs-input-sizes">
        <input name="friend[fullname]" value="<?= isset($friend['fullname'])? $friend['fullname'] : '' ?>" class="input-xxlarge" type="text" />
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Mail</label>
      <div class="controls docs-input-sizes">
        <input name="friend[mail]" value="<?= isset($friend['mail'])? $friend['mail'] : '' ?>" class="input-xxlarge" type="text" />
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="inlineCheckboxes">Nhóm</label>
      <div class="controls">
        <?php if ($act == 'edit') $fgroups = $friend['friend_groups']; ?>

        <?php foreach ($groups as $group): ?>
        <label class="checkbox inline">
          <?php 
            // Kiểm tra xem friend có thuộc nhóm hay không
            if ($act == 'edit') {
              $of_group = false; 
              foreach ($fgroups as $fgroup) {
                if ($fgroup['friend_group_id'] == $group['friend_group_id']) {
                  $of_group = true;
                  break;
                }
              }
            } else {
              $of_group = false;
            }
          ?>
          <input type="checkbox" name="friend[friend_group_ids][]" <?= ($of_group)? 'checked="checked"' : '' ?> value="<?= $group['friend_group_id'] ?>">
          <?= $group['name'] ?>
        </label>
        <?php endforeach; ?>
      </div>
    </div>
    <div class="form-actions">
      <button type="submit" name="act" value="<?= ($act == 'add_new')? 'add' : 'update' ?>" class="btn btn-primary">Lưu lại</button>
      <button class="btn">Hủy bỏ</button>
    </div>
  </fieldset>
</form>